(($) -> 
	jQuery.fn.rtTickets = ->

		$editBtn = $('''
			<div id='edit' style='position: absolute; display: table; text-align: right; color: RED; margin-top: -2px;'>
				<font style='font-size: 15px;'>
					[Edit]
				</font>
			</div>
		''')

		$subjInput = $('''
		<p style="display: none" id="genTextWrap">
			<input type="text" id="genTextArea"/>
			<br />
			<input type="submit" class="saveBtn" value="Save" />
			<input type="submit" class="cancelBtn" value="Cancel">
		</p>
		''')
		
		Deep = true

		# Define a regex to match a ticket's serial number
		serialNum = /serial_num=(\d+)/
		rt_link = "/rt/webrt.cgi"

		# Control flag that sets condition to displaying forms
		ctrlFlag = false
		hoverFlag = false

		$('font >table >tbody >tr').each ->
			$this = $(@)
			href = $('a', $this).attr 'href'
			if (href.match serialNum)?[1]
				$('#subject, #requestor', $this).addClass('inputTypeCell')

		$(@).on 'hover', 'td.inputTypeCell', ->

			if !$(@).children('#edit').length then hoverFlag = false

			if !hoverFlag & !ctrlFlag
				hoverFlag = true

				# Delay this so that the edit button isn't so flashy when mouse is traversing across
				# the table quickly
				cellPos = $(@).position()
				$(@).append($editBtn.clone())
				$(@).children('#edit')
					.css(
						width: $(@).innerWidth()
						top: cellPos.top
						left: cellPos.left
					).hide(0).delay(500).fadeIn(100)

			else
				$(@).children('#edit').children('font')
				.mousemove ->
					$(@).css('text-decoration': 'underline' )
					return
				.mouseleave ->
					$(@).css('text-decoration': 'none')
					return

				return

			$(@).on 'mouseleave', 'td.inputTypeCell', ->
				if hoverFlag 
					$(@).children('#edit').remove()
					hoverFlag = false
				else
				return  

			$(@).on 'click', '#edit', ->

				hoverFlag = true
				$host = $(@).parent()
				ctrlWidth = $host.width()

				# Get the serial number of the current selected ticket to be operated
				# Since each tr has a ticket id, the parentNode id gives the serial number (or ticket)
				sn = $host.parent().attr('id')

				# Check to see if this control flag is marked. If True, then there is always a modifying control
				# somewhere on this page, so don't do anything. If false, then add the controls. 
				if !ctrlFlag

					# Since we've added a control, set the control flag to true in order to restrict
					# any more controls to be added on to the webpage
					ctrlFlag = true

					# Get the current value inside the td cell (for comparison)
					# (May not be necessary)
					currentValue = $host.children('font').text()

					# Make a copy of the current value inside the td cell and replace it
					# with the pre-generated text-field box
					$textVal = $host.children('font').clone Deep

					$host.empty().append($subjInput.clone())
					.css(width: ctrlWidth)
					# Show the control (since it was set to display: none")
					.children().fadeIn(300)
					# Set the text inside the input box to currentValue and then focus the input box
					$host.children('P').children('INPUT[id="genTextArea"]').val(currentValue).focus()

				# Event Hanlder for when the save button is clicked
				$host.children('P').on 'click', 'INPUT.saveBtn', ->

					# Get the subject in the previous inbox (which is the new subject to be saved)
					newValue = $(@).prevAll('INPUT').val()

					# If old subject and new subject are identical, don't send GET
					if newValue.toString() isnt $textVal.text().toString()
						$().dbg 'Waiting for server response..'
						$textVal.children('A').text("#{newValue} [..]")

					switch $(@).parent().parent().attr('id')
						when 'subject' 
							$.get rt_link, { serial_num : sn, do_req_subject : true, subject: newValue}, (data) ->
								exp = /Request .* subject has been changed to ".*"./
								$('#hiddenWrapper').html "<font> #{s = data.match(exp)[0].toString()} </font>"
								$().dbg s
								$textVal.text s.match(/"(.*?)"/)[1]
								return false
						when 'requestor'
							$.get rt_link, { serial_num : sn, do_req_user : true, recipient: newValue}, (data) ->
								$textVal.children('A').text newValue
								$().dbg 'Requestor has been changed'
								return false
						else

					$(@).parent().replaceWith($textVal)
					ctrlFlag = false
					return false

				# Event Hanlder for when the cancel button is clicked
				$host.children('P').on 'click', 'INPUT.cancelBtn', ->
					$(@).parent().replaceWith($textVal)
					ctrlFlag = false
					return false
				return

)(jQuery)
